﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADC.BL;
using ADC.DAL;
using ADC.Utilites;

namespace ADC.Web.Admin
{
    public partial class ManageProduct : System.Web.UI.Page
    {
        log4net.ILog logger = log4net.LogManager.GetLogger("ManageProduct");
        private int _mode;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitData();
            }
        }

        private void InitData()
        {
            // Check if the mode is existing
            if (Request.Params["mode"] != null)
            {

                //load data for DropDownListProgrameType
                List<DAL.ProductType> productType = ProductTypeFactory.SelectAll();
                foreach (DAL.ProductType pt in productType)
                {
                    DropDownListProdcutType.Items.Add(new ListItem(pt.Title, pt.ProductTypeID + ""));
                }

                try
                {
                    _mode = Convert.ToInt32(Request.Params["mode"].ToString());

                    if (_mode.Equals(1)) // Edit Mode
                    {
                        lblHeader.Text = "Cận nhật sản phẩm";
                        LinkButtonAddNew.Text = "Update";
                        if (Request.Params["ID"] != null)
                        {
                            var ID = Convert.ToInt32(Request.Params["ID"].ToString());
                            var product = ProductFactory.GetByID(ID);
                            txtName.Text = product.Name;
                            txtDescription.Text = product.Description;
                            txtModel.Text = product.Model;
                            txtPower.Text = product.Power;
                            txtMadeIn.Text = product.MadeIn;
                            txtTech.Text = product.Tech;
                            txtPrice.Text = product.Price;
                            txtWaranty.Text = product.Warranty;
                            if (product.ProductType != null)
                            {
                                DropDownListProdcutType.SelectedValue = product.ProductType.ToString();
                            }
                            
                            if (product.IsHot != null)
                            {
                                CheckBoxIsHot.Checked = Convert.ToBoolean(product.IsHot);
                            }
                            else CheckBoxIsHot.Checked = false; 
                            
                            ImageProduct.ImageUrl = "~/" + product.ImagePath;
                        }
                    }
                    else // New mode
                    {
                        lblHeader.Text = "Thêm sản phẩm";
                        LinkButtonAddNew.Text = "Thêm mới";
                    }

                }
                catch (Exception ex)
                {
                    logger.Error(ex.ToString());
                    if (GlobalInfo.IsEnableDebugMode) throw;
                }
            }

        }

        protected void LinkButtonAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.Params["mode"] == null) return;
                _mode = Convert.ToInt32(Request.Params["mode"]);
                var productType = Convert.ToInt32(Request.Params["Type"].ToString());
                if (_mode.Equals(1)) // Edit 
                {
                    if (Request.Params["ID"] != null)
                    {
                        var ID = Convert.ToInt32(Request.Params["ID"].ToString());

                        var product = ProductFactory.GetByID(ID);
                        if (product == null) return;
                        product.Name = txtName.Text;
                        product.Description = txtDescription.Text;
                        product.Model = txtModel.Text;
                        product.MadeIn = txtMadeIn.Text;
                        product.Price = txtPrice.Text;
                        product.Tech = txtTech.Text;
                        product.Warranty = txtWaranty.Text;
                        product.IsHot = CheckBoxIsHot.Checked;
                        product.ProductType = Convert.ToInt32(DropDownListProdcutType.SelectedValue);

                        //Upload the file 

                        if (FileUploadProduct.HasFile)
                        {
                            FileUploadProduct.SaveAs(Request.PhysicalApplicationPath + Utilites.Common.IMAGE_PRODUCT + FileUploadProduct.FileName);
                            product.ImagePath = Utilites.Common.IMAGE_PRODUCT + FileUploadProduct.FileName;

                        }

                        ProductFactory.Update(product);

                        Response.Redirect("ListProduct.aspx", false);
                    }
                }
                else // New mode
                {
                    var product = new DAL.Product();

                    product.Name = txtName.Text;
                    product.Description = txtDescription.Text;
                    product.Model = txtModel.Text;
                    product.MadeIn = txtMadeIn.Text;
                    product.Price = txtPrice.Text;
                    product.Tech = txtTech.Text;
                    product.Warranty = txtWaranty.Text;
                    product.ProductType = Convert.ToInt32(DropDownListProdcutType.SelectedValue);
                    product.IsHot = CheckBoxIsHot.Checked;
                    product.CreatedDate = DateTime.Now;
                    product.CreatedBy = ((DAL.User)Session["UserInfo"]).UserID;
                    product.ModifiedDate = DateTime.Now;

                    if (FileUploadProduct.HasFile)
                    {
                        FileUploadProduct.SaveAs(Request.PhysicalApplicationPath + Utilites.Common.IMAGE_PRODUCT + FileUploadProduct.FileName);
                        product.ImagePath = Utilites.Common.IMAGE_PRODUCT + FileUploadProduct.FileName;
                    }

                    var result = ProductFactory.Insert(product);

                    if (result.ProductID > 0)
                    {
                        Response.Redirect("ListProduct.aspx", false);
                    }
                    else
                    {
                        lblErrorMessage.Visible = true;
                        lblErrorMessage.Text = Common.INSERT_FAIL;
                    }
                }

            }
            catch (Exception ex)
            {
                lblErrorMessage.Visible = true;
                lblErrorMessage.Text = Common.UPDATE_FAIL;
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }
    }
}

